//
//  Macro.swift
//  Fanmugua
//
//  Created by MK on 2021/3/28.
//  Copyright © 2021 taolang. All rights reserved.
//

import UIKit

func MKLog<T>(_ message:T) {
//    #if DEBUG
        print(message)
//    #endif
}

func kLog<T>(_ message: T, fileName: String = #file, lineNumber: Int = #line){
    print("file:\(fileName) line:\(lineNumber) \(message)")
//    #if DEBUG
//    let str = ((fileName as NSString).pathComponents.last! as NSString).replacingOccurrences(of: ".swift", with: "")
//    print("[\(NSDate.mk_currentTimestam()!)  \(str)  line:\(lineNumber)] : \(message)")
//    #endif
}

let SCREEN_SIZE         = UIScreen.main.bounds.size
let SCREEN_WIDTH        = UIScreen.main.bounds.size.width
let SCREEN_HEIGHT       = UIScreen.main.bounds.size.height
let SCREEN_BOUNDS       = UIScreen.main.bounds
let SCREEN_SCALE        = UIScreen.main.scale
let ONE_PIXEL_HEIGHT    = CGFloat(1.0/UIScreen.main.scale)

let SCREEN_NATIVE_SIZE:CGSize       = UIScreen.main.nativeBounds.size
let SCREEN_NATIVE_WIDTH:CGFloat     = UIScreen.main.nativeBounds.size.width
let SCREEN_NATIVE_HEIGHT:CGFloat    = UIScreen.main.nativeBounds.size.height

let IS_IPHONE       = Bool(UIDevice.current.userInterfaceIdiom == .phone)
let IS_IPAD         = Bool(UIDevice.current.userInterfaceIdiom == .pad)

let IS_IPHONE_4:Bool        = (SCREEN_WIDTH == CGFloat(320) && SCREEN_HEIGHT == CGFloat(480))
let IS_IPHONE_5:Bool        = (SCREEN_NATIVE_WIDTH == CGFloat(640) && SCREEN_NATIVE_HEIGHT == CGFloat(1136))
let IS_IPHONE_678:Bool      = (SCREEN_NATIVE_WIDTH == CGFloat(750) && SCREEN_NATIVE_HEIGHT == CGFloat(1334))
let IS_IPHONE_678_PLUS:Bool = (SCREEN_NATIVE_WIDTH == CGFloat(1242) && SCREEN_NATIVE_HEIGHT == CGFloat(2208))
let IS_IPHONE_X_XS:Bool     = (SCREEN_NATIVE_WIDTH == CGFloat(1125) && SCREEN_NATIVE_HEIGHT == CGFloat(2436))
let IS_IPHONE_XSMAX:Bool    = (SCREEN_NATIVE_WIDTH == CGFloat(1242) && SCREEN_NATIVE_HEIGHT == CGFloat(2688))
let IS_IPHONE_XR:Bool       = (SCREEN_NATIVE_WIDTH == CGFloat(828) && SCREEN_NATIVE_HEIGHT == CGFloat(1792))
let IS_IPHONE_12MINI:Bool   = (SCREEN_NATIVE_WIDTH == CGFloat(1080) && SCREEN_NATIVE_HEIGHT == CGFloat(2340))
let IS_IPHONE_12:Bool       = (SCREEN_NATIVE_WIDTH == CGFloat(1170) && SCREEN_NATIVE_HEIGHT == CGFloat(2532))
let IS_IPHONE_12MAX:Bool    = (SCREEN_NATIVE_WIDTH == CGFloat(1284) && SCREEN_NATIVE_HEIGHT == CGFloat(2778))
//Macro
func isIPhoneXSeries() -> Bool{
    if #available(iOS 11.0, *){
        let window:UIWindow = (UIApplication.shared.delegate?.window)!!
        if window.safeAreaInsets.bottom > 0 {
            return true
        }
    }
    return false
}

let IS_IPHONE_XX                        = isIPhoneXSeries()

let STATUS_BAR_HEIGHT:CGFloat           = (IS_IPHONE_XX ? 44 : 20)
let NAVGATION_BAR_HEIGHT:CGFloat        = 44
let NAVGATION_VIEW_HEIGHT:CGFloat       = STATUS_BAR_HEIGHT + NAVGATION_BAR_HEIGHT
let NAVGATION_VIEW_DIFF_HEIGHT:CGFloat  = (IS_IPHONE_XX ? 24 : 0)
let TABBAR_HEIGHT:CGFloat               = (IS_IPHONE_XX ? 49+34 : 49)

let SCREEN_SAVE_TOP_HEIGHT:CGFloat      = (IS_IPHONE_XX ? 44 : 0)
let SCREEN_SAVE_BOTTOM_HEIGHT:CGFloat   = (IS_IPHONE_XX ? 34 : 0)

let SCREEN_SAFE_HEIGHT:CGFloat = (SCREEN_HEIGHT - SCREEN_SAVE_TOP_HEIGHT - SCREEN_SAVE_BOTTOM_HEIGHT)
let SCREEN_MAIN_HEIGHT:CGFloat = (SCREEN_HEIGHT - NAVGATION_VIEW_HEIGHT - SCREEN_SAVE_BOTTOM_HEIGHT)

//#define adjust(a) [HYUtils deviceFloat:a]

func isSimulator() -> Bool {
    #if TARGET_OS_SIMULATOR
        return true
    #else
        return false
    #endif
}

let kCGRectZero     = CGRect(x: 0, y: 0, width: 0, height: 0)
let kCGSizeZero     = CGSize(width: 0, height: 0)
let kCGPointZero    = CGPoint(x: 0, y: 0)

let kNotificationCenter = NotificationCenter.default
let kFileManager    = FileManager.default
let kUserDefaults   = UserDefaults.standard
let kUUIDString     = UIDevice.current.identifierForVendor?.uuidString




typealias VoidBlock = () -> Void
typealias BoolBlock = (Bool) -> Void
typealias IntBlock = (Int) -> Void
typealias NSIntegerBlock = (NSInteger) -> Void

func RGBA(_ r:CGFloat, _ g:CGFloat, _ b:CGFloat, _ a:CGFloat = 1.0) -> UIColor {
    return UIColor(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: a)
}
// 随机颜色
var COLOR_RANDOM:UIColor {
    return RGBA(CGFloat(arc4random() % 255), CGFloat(arc4random() % 255), CGFloat(arc4random() % 255))
}

func FONT_SYS(_ size:CGFloat) -> UIFont {
    return UIFont.systemFont(ofSize: size)
}

func FONT_BOLD(_ size:CGFloat) -> UIFont {
    return UIFont.boldSystemFont(ofSize: size)
}

/// 沙盒路径
let DOCUMENT_DIR_PATH:String = (NSSearchPathForDirectoriesInDomains(
                                    FileManager.SearchPathDirectory.documentDirectory,
                                    FileManager.SearchPathDomainMask.userDomainMask, true
                                ).last! as String)

/// 获取指定时间字格式化
/// - Parameters:
///   - dateFormat: 格式 例：yy-mm-dd HH:MM:SS
///   - time: date
/// - Returns: string
func DATE_STRING(_ dateFormat:String, _ time:Date = Date()) -> String {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = dateFormat
    return dateFormatter.string(from: time)
}


/// 确保在主线程执行
/// - Parameter work: 执行内容 block
public func ensureMainThread(execute work: @escaping @convention(block) () -> Void){
    if Thread.isMainThread {
        work();
    } else {
        DispatchQueue.main.async {
            work();
        }
    }
}


/// 定时执行
/// - Parameters:
///   - timeInterval: 间隔时间
///   - repeatCount: 执行次数，0:表示无限循环
///   - handler: 执行回调
public func dispatchTimer(timeInterval: Double, repeatCount: Int, handler: @escaping(DispatchSourceTimer?, Int, Bool) -> Void){
    let forever = repeatCount == 0
    var count = repeatCount
    let timer = DispatchSource.makeTimerSource(flags: [], queue: DispatchQueue.main)
    timer.schedule(deadline: .now(), repeating: timeInterval)
    timer.setEventHandler {
        count = forever ? count + 1 : count - 1
        DispatchQueue.main.async {
            handler(timer, count, count < 0)
        }
        if count < 0 {
            timer.cancel()
        }
    }
    timer.resume()
}

